package com.syh.jn.scm.infrastructure.repository.impl.supplier.base;

import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.infrastructure.repository.AbstractAggregateRepository;
import com.syh.jn.scm.domain.supplier.entity.base.SalesTeam;
import com.syh.jn.scm.domain.supplier.repository.base.SalesTeamRepository;
import com.syh.jn.scm.dto.supplier.base.salesTeam.SalesTeamVo;
import com.syh.jn.scm.infrastructure.repository.model.base.SalesTeamModel;
import com.syh.jn.scm.infrastructure.repository.model.base.SalesmanModel;
import com.syh.jn.scm.infrastructure.repository.utils.SQLAliasGenerateUtil;
import com.syh.jn.scm.infrastructure.wrapper.BaseResolve;
import com.syh.jn.scm.infrastructure.wrapper.ResolveToSQL;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class SalesTeamRepositoryImpl extends AbstractAggregateRepository<SalesTeam, SalesTeamModel, Long> implements SalesTeamRepository {
  @Override
  protected SQL getViewSql() {
    return new SQL() {{
      SELECT(
              SQLAliasGenerateUtil.aliasGenerate(SalesTeamModel.class, "salesTeam", null),
              "salesMan.name as salesmanName",
              "salesMan.mobile as salesmanMobile"
      );
      FROM("T_BASE_SALES_TEAM salesTeam");
      LEFT_OUTER_JOIN("T_BASE_SALESMAN salesMan on salesTeam.SALESMAN_ID = salesMan.ID");
    }};
  }

  @Override
  protected Class getViewObject() {
    return SalesTeamVo.class;
  }

  @Override
  protected BaseResolve getViewWrapperResolve(Wrapper wrapper) {
    Map<String,String> params = new HashMap<>();
    params.put("name","salesTeam.NAME");
    params.put("status","salesTeam.STATUS");
    params.put("id","salesTeam.ID");
    params.put("businessEntityId","salesTeam.BUSINESS_ENTITY_ID");
    ResolveToSQL parse = new ResolveToSQL(wrapper, (propertyName) -> {
      return params.get(propertyName);
    });
    return parse;
  }
  /**
   * 重写add
   * @param salesTeam
   */
  @Override
  public Long add(SalesTeam salesTeam){
    Long teamId = super.add(salesTeam);
    //判断是否有选择销售代表
    if(salesTeam.getSalesmanId() == null){
      return teamId;
    }
    //根据销售团队所选代表维护销售代表的销售团队字段
    this.updateSalesman(salesTeam.getSalesmanId(), teamId);

    return teamId;
  }

  /**
   * 重写update
   * @param salesTeam
   */
  @Override
  public void update(SalesTeam salesTeam){
    super.update(salesTeam);

    //根据销售团队所选代表维护销售代表的销售团队字段
    this.updateSalesman(salesTeam.getSalesmanId(), salesTeam.getId());
  }

  /**
   * 根据销售团队所选代表维护销售代表的销售团队字段
   * @param manId
   * @param teamId
   */
  private void updateSalesman(Long manId, Long teamId){
    //根据销售团队Model所属的销售代表维护销售代表Model所属的销售团队
    //查找当前销售团队所属的销售代表
    StringBuffer hql =  new StringBuffer();
    hql.append("from SalesmanModel model where model.id = ?");
    List<SalesmanModel> salesmans = (List<SalesmanModel>)this.hibernateTemplate.find(hql.toString(), manId);

    salesmans.stream().forEach(salesman -> {
      //维护销售团队字段
      salesman.setSalesTeamId(teamId);
      this.hibernateTemplate.update(salesman);
    });
  }
}
